package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 陈辉
 * @data 2023 17:32
 */
@Service
@Slf4j
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 素材上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //1. 校验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 上传文件到minio
        String fileName = UUID.randomUUID().toString();
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));

        String finalFileName = fileName + suffix;
        String imgFile = null;

        try {
            imgFile = fileStorageService.uploadImgFile("", finalFileName, multipartFile.getInputStream());
            //3. 存入数据库
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUrl(imgFile);
            wmMaterial.setCreatedTime(new Date());
            save(wmMaterial);

            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 素材列表：条件分页查询
     *
     * @param materialDto
     * @return
     */
    @Override
    public ResponseResult queryByPageAndCondition(WmMaterialDto materialDto) {
        //1. 参数校验
        if (materialDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        materialDto.checkParam();

        //2. 业务操作 -- 条件分页查询
        IPage ipage = new Page(materialDto.getPage(),materialDto.getSize());
        page(ipage, Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getIsCollection,materialDto.getIsCollection()));

        //3. 处理结果返回
        List list = ipage.getRecords();     //当前页数据
        long total = ipage.getTotal();      //总记录条数

        ResponseResult result = new PageResponseResult((int)ipage.getCurrent(),
                (int)ipage.getSize(),(int)total);

        result.setData(list);

        return result;
    }
}
